package server

import (
	"common/database/config"
	"context"
	"test_question_srv/internal/models/dao"
	pb "test_question_srv/proto"
)

type Server struct {
	pb.UnimplementedQuestionServer
}

func (c *Server) List(ctx context.Context, request *pb.ListRequest) (*pb.ListResponse, error) {
	var cate []*dao.Cate
	list := GetList(cate, 0)
	return &pb.ListResponse{List: list}, nil
}

func GetList(cat []*dao.Cate, pid int) []*pb.List {
	var cate []*dao.Cate
	var sli []*pb.List
	config.DB.Model(&dao.Cate{}).Where("pid=?", pid).Find(&cate)
	for _, d := range cate {
		var one []*dao.Cate
		config.DB.Where("pid=?", d.ID).Find(&one)
		two := pb.List{
			Id:        int64(d.ID),
			ClassName: d.ClassName,
			Pid:       int64(d.Pid),
			List:      GetList(one, int(d.ID)),
		}
		sli = append(sli, &two)
	}
	return sli
}
